//
//  OrderRealDescTableCell.m
//  LoveFreshBeen
//
//  Created by vinsent on 16/10/21.
//  Copyright © 2016年 魏根. All rights reserved.
//

#import "OrderRealDescTableCell.h"
#import "MyorderViewModel.h"
#import "OrderGoodModel.h"
#import "FeeModel.h"
#import <Masonry.h>

@interface OrderRealDescTableCell ()
@property(nonatomic,strong) UILabel *titleLab;
@property(nonatomic,strong) UILabel *sendMoneyLab;
@property(nonatomic,strong) UILabel *serveMoneyLab;
@property(nonatomic,strong) UILabel *chargeLab;
@property(nonatomic,strong) UILabel *realPayLab;


@property(nonatomic,assign)UILabel *maxLastLab;
@property(nonatomic,assign)UILabel *maxLast;

@end

@implementation OrderRealDescTableCell

- (instancetype)init {
    if (self = [super init]) {
        
        [self setUpFixSubViewsUI];
//        [self setUpRicketySubViewsUI];
    }
    return self;
}


- (void)drawRect:(CGRect)rect {
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    path.lineWidth = 0.5;
    [[UIColor lightGrayColor] set];
    
    [path moveToPoint:CGPointMake(8, CGRectGetMaxY(self.titleLab.frame) + 7)];
    [path addLineToPoint:CGPointMake([UIScreen mainScreen].bounds.size.width, CGRectGetMaxY(self.titleLab.frame) + 7)];
    
    
    
    [path stroke];
    
    //写在drawRect中,在init方法之后调用,而且只会调用一次,
     [self setUpRicketySubViewsUI];
}


- (void)setUpFixSubViewsUI {
    UILabel *title = [[UILabel alloc] init];
    [title setText:@"费用明细"];
    [title setTextColor:[UIColor lightGrayColor]];
    [title setFont:[UIFont systemFontOfSize:12]];
    [self.contentView addSubview:title];
    self.titleLab = title;
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.equalTo(self.contentView).offset(8);
    }];
    [title sizeToFit];
    
    
}
#define lastMargin 22
#define firstMargin 28
#define margin 35

- (void)setUpRicketySubViewsUI {
    
    int num = 0;
    for (OrderGoodModel *orderGoood in [MyorderViewModel sharedMyorder].myorders[self.index].myorderModel.order_goods) {
        UILabel *lab = [[UILabel alloc] init];
        [lab setText:orderGoood.name];
        [self setLable:lab];
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.titleLab);
            make.centerY.equalTo(self.titleLab).offset(self.titleLab.center.y + margin * num + firstMargin);
        }];
        
        UILabel *numLab = [[UILabel alloc] init];
        [numLab setText:[NSString stringWithFormat:@"x%@",orderGoood.goods_nums]];
        [self setLable:numLab];
        [numLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.contentView.mas_trailing).offset(-180);
            make.centerY.equalTo(lab);
        }];
        
        UILabel *price = [[UILabel alloc] init];
       float pri = [orderGoood.real_price floatValue];
        NSMutableString *str = [NSMutableString stringWithFormat:@"$%.1f",pri];
        [price setText:str];
        [self setLable:price];
        [price mas_makeConstraints:^(MASConstraintMaker *make) {
            make.trailing.equalTo(self.contentView).offset(-8);
            make.centerY.equalTo(lab);
        }];
        
        self.maxLastLab = lab;
        num ++;
    }
    
    
    num = 1;
    for (FeeModel *fee in [MyorderViewModel sharedMyorder].myorders[self.index].myorderModel.fee_list) {
        
        
        UILabel *sendMoney = [[UILabel alloc] init];
        [sendMoney setText:fee.text];
        [self setLable:sendMoney];
        [sendMoney mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.titleLab);
            make.centerY.equalTo(self.maxLastLab.mas_bottom).offset(lastMargin * num);
        }];

         UILabel *sendMoneylab = [[UILabel alloc] init];
        [sendMoneylab setText:[NSString stringWithFormat:@"$%.1f",[fee.value floatValue]]];
        [self setLable:sendMoneylab];
        [sendMoneylab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.trailing.equalTo(self.contentView).offset(-8);
            make.centerY.equalTo(sendMoney);
        }];
        self.maxLast = sendMoneylab;
        num ++;
    }
    
    UILabel *real = [[UILabel alloc] init];
    [real setText:@"实付"];
    [self setLable:real];
    [real mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.contentView).offset(-100);
        make.centerY.equalTo(self.maxLast.mas_bottom).offset(margin);
    }];
    
    UILabel *realPay = [[UILabel alloc] init];
    [realPay setText:[NSString stringWithFormat:@"$%@",[MyorderViewModel sharedMyorder].myorders[self.index].myorderModel.real_amount]];
    [self setLable:realPay];
    [realPay mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.contentView).offset(-8);
        make.centerY.equalTo(real);
    }];
}

- (void) setLable:(UILabel *)lab {
    [lab setFont:[UIFont systemFontOfSize:14]];
    [lab setTextColor:[UIColor darkGrayColor]];
    [self.contentView addSubview:lab];
    [lab sizeToFit];
}


@end
























